// Category页面数据
import { ref, onMounted } from "vue";
import { getCategroy } from "@/apis/categroy";
// onBeforeRouteUpdate是在路由变化之前钩子
import { useRoute, onBeforeRouteUpdate } from "vue-router";

export function useCategory() {
    const categoryData = ref([]);
    // 获取route
    const route = useRoute();
    // 请求函数
    const getCategroyFunc = async (id = route.params.id) => {
        const res = await getCategroy(id);
        categoryData.value = res.result;
    };

    // 挂载获取数据
    onMounted(() => getCategroyFunc());

    // 路由变化之前发生请求
    onBeforeRouteUpdate((to) => {
        getCategroyFunc(to.params.id);
    });

    return {
        categoryData,
    };
}
